package com.example.home.vm

import androidx.lifecycle.viewModelScope
import com.example.common.vm.BaseViewModel
import com.example.home.intent.HomeViewIntent
import com.example.home.model.HomeRepository
import com.example.home.state.HomeUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(private val repository: HomeRepository):BaseViewModel<HomeViewIntent>(){

    private val _uiState = MutableStateFlow<HomeUiState>(HomeUiState.LoadEmpty)
    val uiState:StateFlow<HomeUiState> = _uiState.asStateFlow()

    override fun dispatchIntent(intent: HomeViewIntent) {
        when(intent){
            is HomeViewIntent.LoadVideoType->loadSimpleVideoType()
            is HomeViewIntent.LoadSimpleVideoListById->loadSimpleViewById(intent.channelId,intent.page,intent.pageSize)
            is HomeViewIntent.LoadRecommendSimpleVideoList->loadRecommendSimpleVideo(intent.page,intent.pageSize)
        }
    }
    //分类模块
    private fun loadSimpleVideoType(){
        viewModelScope.launch(Dispatchers.IO){
               try {
                   repository.getSimpleTypes().collect{
                       if(it.isNotEmpty()){
                           _uiState.value = HomeUiState.LoadSimpleTypeSuccess(it)
                       }else{
                           _uiState.value = HomeUiState.LoadSimpleTypeFail("没有这个分类")
                       }
                   }
               }catch (e:Exception){
                   e.printStackTrace()
                   _uiState.value = HomeUiState.LoadSimpleTypeFail("请求失败"+e.message)
               }finally {
                   cancel()
               }
        }
    }

    //simple视频
    private fun loadSimpleViewById(channelId:String,page:Int,pageSize:Int){
        viewModelScope.launch(Dispatchers.IO){
            try {
                repository.getSimpleVideoByChannelId(channelId,page,pageSize).collect{
                    if(it.isNotEmpty()){
                        _uiState.value = HomeUiState.LoadSimpleVideoSuccess(it)
                    }else{
                        _uiState.value = HomeUiState.LoadSimpleVideoFail("没有这个视频！！！")
                    }
                }
            }catch (e:Exception){
                e.printStackTrace()
                _uiState.value = HomeUiState.LoadSimpleVideoFail("请求失败!!!!!")
            }finally {
                cancel()
            }
        }
    }
    //获取推荐视频
    private fun loadRecommendSimpleVideo(page:Int,pageSize:Int){
        viewModelScope.launch(Dispatchers.IO){
            try {
                repository.getRecommendVideo(page,pageSize).collect{
                    if(it.isNotEmpty()){
                        _uiState.value = HomeUiState.LoadSimpleVideoSuccess(it)
                    }else{
                        _uiState.value = HomeUiState.LoadSimpleVideoFail("没有这个推荐视频！！！！！！！！")
                    }
                }
            }catch (e:Exception){
                e.printStackTrace()
                _uiState.value = HomeUiState.LoadSimpleVideoFail("请求失败!!!!!")
            }finally {
                cancel()
            }
        }
    }


}